home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / gamesmaster / source / c / random.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-08  |  3.3 KB  |  106 lines

  1. /*
  2. ** Random Demo
  3. ** -----------
  4. ** Demonstrates the randomness of functions FastRandom() and SlowRandom().
  5. ** Given the speed of FastRandom I think you will be quite impressed with
  6. ** it's results.  It gets all the numbers and gives a very even balance.
  7. ** The sequence of numbers is however not the best, so only use it when
  8. ** speed is critical.
  9. **
  10. ** In case you are wondering the FastRandom() routine is just 10 assembler
  11. ** instructions including the re-seed and range div...  Wow!
  12. **
  13. ** Compiles under SAS/C.
  14. */
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <proto/games.h>
  19. #include <proto/exec.h>
  20.  
  21. struct GMSBase *GMSBase;
  22. int YesNo;
  23. ULONG loop, Iterations, UserRange;
  24. UWORD *NumArray, Number;
  25.  
  26. /*=========================================================================*/
  27. /*                               MAIN CODE                                 */
  28. /*=========================================================================*/
  29.  
  30. void main(void)
  31. {
  32.    struct GamesLibrary *GMSBase = (struct GamesLibrary *)
  33.        OpenLibrary("games.library", 0);
  34.    if (GMSBase == NULL) exit(FALSE);
  35.  
  36.    printf(" Random Number Demo");
  37.    printf("\n------------------");
  38.    printf("\nThis program demonstrates the randomness of the games.library functions");
  39.    printf("\nFastRandom() and SlowRandom().  The FastRandom() is just 10 assembler");
  40.    printf("\ninstructions and SlowRandom() is about twice that.  Both give very good");
  41.    printf("\nresults, as you will see...\n\n");
  42.  
  43.    do {
  44.         printf("Please enter a range between 1 and 50 : ");
  45.         fflush(stdin);
  46.         scanf("%d", &UserRange);
  47.       }
  48.    while (UserRange > 500 || UserRange < 1);
  49.  
  50.    printf("Enter the amount of iterations (Try 500000) : ");
  51.    fflush(stdin);
  52.    scanf("%d", &Iterations);
  53.  
  54.    printf("Do you want to view all %d generated numbers (Y/n)? ",Iterations);
  55.    fflush(stdin);
  56.    if (getchar() == 'n') YesNo = 1;
  57.  
  58. /*=========================================================================*/
  59. /*                     GENERATE FAST RANDOM NUMBERS                        */
  60. /*=========================================================================*/
  61.  
  62.    NumArray = calloc(UserRange,sizeof(UWORD));
  63.  
  64.    printf("\nPlease wait while generating %d FastRandom() numbers...\n",Iterations);
  65.  
  66.    loop = 0;
  67.    do {
  68.       Number = FastRandom(UserRange);
  69.       if (YesNo == 0) printf("%d ", Number);
  70.       NumArray[Number] += 1;
  71.    }
  72.    while (++loop < Iterations);
  73.  
  74.    printf("\nResults are:\n");
  75.  
  76.    for (loop=0; loop<UserRange; loop++) {
  77.       printf("%4d: %d\n", loop, NumArray[loop]);
  78.       NumArray[loop] = 0;
  79.    }
  80.  
  81. /*=========================================================================*/
  82. /*                     GENERATE SLOW RANDOM NUMBERS                        */
  83. /*=========================================================================*/
  84.  
  85.    printf("\nPlease wait while generating %d SlowRandom() numbers...\n",Iterations);
  86.  
  87.    loop = 0;
  88.    do {
  89.       Number = SlowRandom(UserRange);
  90.       if (YesNo == 0) printf("%d ",Number);
  91.       NumArray[Number]++;
  92.    }
  93.    while (++loop < Iterations);
  94.  
  95.    printf("\nResults are:\n");
  96.  
  97.    for (loop=0; loop<UserRange; loop++) {
  98.       printf("%4d: %d\n", loop, NumArray[loop]);
  99.    }
  100.  
  101.    CloseLibrary((struct Library *)GMSBase);
  102. }
  103.  
  104. /*=========================================================================*/
  105.  
  106.